using System;
using System.Collections;
using System.IO;
using System.Runtime.CompilerServices;
using UnityEngine;

public class ScreenShotMultiRes : MonoBehaviour
{
	public delegate void ShotScreenEventHandler();

	public Camera[] cameras;

	public Vector2[] resolutions;

	public bool run;

	public static bool waitingForEvent;

	public bool waitForEvent;

	public static bool alwaysRun;

	private string sessionID;

	private Camera captureCamera;

	private int captureCount;

	[method: MethodImpl(32)]
	public static event ShotScreenEventHandler ShotScreenEvent;

	public static void ShotScreen()
	{
		if (ScreenShotMultiRes.ShotScreenEvent != null)
		{
			ScreenShotMultiRes.ShotScreenEvent();
		}
	}

	private void Start()
	{
		sessionID = DateTime.Now.ToString("yyyyMMddHHmmss");
		InitCaptureCamera();
		ScreenShotMultiRes.ShotScreenEvent = (ShotScreenEventHandler)Delegate.Combine(ScreenShotMultiRes.ShotScreenEvent, new ShotScreenEventHandler(RunAtEvent));
	}

	private void OnDestroy()
	{
		ScreenShotMultiRes.ShotScreenEvent = (ShotScreenEventHandler)Delegate.Remove(ScreenShotMultiRes.ShotScreenEvent, new ShotScreenEventHandler(RunAtEvent));
	}

	private void RunAtEvent()
	{
		if (waitForEvent || alwaysRun)
		{
			waitForEvent = false;
			waitingForEvent = waitForEvent;
			StartCoroutine(Run());
		}
	}

	private void Update()
	{
		if (Input.GetKeyDown("space"))
		{
			run = true;
		}
		waitingForEvent = waitForEvent;
		if (run)
		{
			StartCoroutine(Run());
			run = false;
		}
	}

	private IEnumerator Run()
	{
		yield return new WaitForEndOfFrame();
		Vector2[] array = resolutions;
		foreach (Vector2 resolution in array)
		{
			ShotScreen(resolution);
		}
		yield return null;
	}

	private void InitCaptureCamera()
	{
		if (captureCamera == null)
		{
			GameObject gameObject = new GameObject("Capture Camera", typeof(Camera));
			captureCamera = gameObject.GetComponent<Camera>();
			captureCamera.enabled = false;
		}
	}

	private void ShotScreen(Vector2 resolution)
	{
		if (cameras.Length == 0)
		{
			cameras = new Camera[1];
			cameras[0] = OrbitCamera.GetInstance().camera;
		}
		captureCount++;
		Camera[] array = cameras;
		foreach (Camera camera in array)
		{
			int num = (int)resolution.x;
			int num2 = (int)resolution.y;
			RenderTexture renderTexture = new RenderTexture(num, num2, 24, RenderTextureFormat.ARGB32);
			if (camera.name == "UI Camera")
			{
				camera.clearFlags = CameraClearFlags.Color;
				camera.backgroundColor = Color.clear;
			}
			camera.targetTexture = renderTexture;
			camera.Render();
			RenderTexture.active = renderTexture;
			Texture2D texture2D = new Texture2D(num, num2, TextureFormat.ARGB32, false);
			texture2D.ReadPixels(new Rect(0f, 0f, num, num2), 0, 0);
			texture2D.Apply();
			string text = sessionID + " " + captureCount + " " + ((int)resolution.x).ToString() + "x" + ((int)resolution.y).ToString() + " " + camera.name;
			File.WriteAllBytes(Application.persistentDataPath + "/" + text + ".png", texture2D.EncodeToPNG());
			Debug.Log("Screenshot saved " + Application.persistentDataPath + "/" + text);
			if (camera.name == "UI Camera")
			{
				camera.clearFlags = CameraClearFlags.Depth;
				camera.backgroundColor = Color.clear;
			}
			camera.targetTexture = null;
			RenderTexture.active = null;
			UnityEngine.Object.DestroyImmediate(renderTexture);
		}
	}
}
